package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.UmsMenu;
import com.itheima.domain.UmsRoleMenuRelation;
import com.itheima.mapper.UmsRoleMenuRelationMapper;
import com.itheima.service.db.UmsRoleMenuRelationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UmsRoleMenuRelationServiceImpl implements UmsRoleMenuRelationService {

    @Autowired
    UmsRoleMenuRelationMapper umsRoleMenuRelationMapper;

    @Override
    public List<UmsRoleMenuRelation> findByRoleId(Long roleId) {
        LambdaQueryWrapper<UmsRoleMenuRelation> qw = new LambdaQueryWrapper<>();
        qw.eq(UmsRoleMenuRelation::getRoleId,roleId);
        return umsRoleMenuRelationMapper.selectList(qw);
    }

    @Override
    public void saveAllocMenu(Long roleId, List<Long> menuIds) {
        // 创建条件构造器
        LambdaQueryWrapper<UmsRoleMenuRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRoleMenuRelation::getRoleId,roleId);
        umsRoleMenuRelationMapper.delete(queryWrapper);

        for (Long menuId : menuIds) {
            UmsRoleMenuRelation umsRoleMenuRelation = new UmsRoleMenuRelation();
            umsRoleMenuRelation.setRoleId(roleId);
            umsRoleMenuRelation.setMenuId(menuId);
            umsRoleMenuRelationMapper.insert(umsRoleMenuRelation);
        }
    }
}
